package by.epam.communalservices.utils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

public class Lang extends SimpleTagSupport {
    static String lang = "";
    static Properties prop = null;
    String key = "";
    String def = "";
    
    public void doTag() throws JspException, IOException {
        if (prop==null) {
            prop = new Properties();
            prop.setProperty("CharacterSetResults", "UTF-8");
            prop.setProperty("CharacterEncoding", "UTF-8"); 
            if (!lang.equals(""))
            try {
                Reader r = new FileReader(lang);
                prop.load(r);
                r.close();
            } catch ( IOException e) {
                //getJspContext().getOut().write(e.toString()); 
            }
        }
        if (prop.containsKey(key)) {
            getJspContext().getOut().write(prop.getProperty(key));
        } else {
            getJspContext().getOut().write(def);
        }
    }
    
    public void setKey( String var ) {
        this.key = var;
        this.def = var;
    }     
    public void setDef( String var ) {
        this.def = var;
    }     
    public static void setLang( String var ) {
        Lang.lang = var;
        prop = null;
    }     
}
